package com.android.internet.webservice;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.ksoap2.transport.Transport;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class WebServiceDemoActivity extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		TextView text = new TextView(this);
		try {
			String f = new CtoFConverter().cToF(0);

			Log.i(this.getClass().getName(), f);

			text.setText("Fahrenheit Value of 0 Celsius  is " + f);

		} catch (Exception ex) {
			Log.e(ex.getClass().getName(), ex.getMessage());
		}
	}

	public class CtoFConverter {
		String action = "http://tempuri.org/CelsiusToFahrenheit";
		String methodName = "CelsiusToFahrenheit";
		String namespace = "http://tempuri.org";
		String url = "http://www.w3schools.com/webservices/tempconvert.asmx";

		public String cToF(int cel) throws Exception {
			Transport httpTransport = new HttpTransportSE(url);
			SoapObject request = new SoapObject(namespace, methodName);
			request.addProperty("Celcius", cel);

			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
					SoapEnvelope.VER11);
			envelope.setOutputSoapObject(request);

			httpTransport.call(action, envelope);
			SoapObject soapResponse = (SoapObject) envelope.bodyIn;
			SoapPrimitive body = (SoapPrimitive) soapResponse
					.getProperty("CelsiusToFahrenheitResult");

			return body.toString();
		}
	}
}